package com.hb.trade.api.routine.controller.project.order;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.center.au.dto.OrgCollectionDTO;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.entity.OrgCollection;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.enums.*;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.calibration.dto.BidWinResultDTO;
import com.hb.core.pojo.project.file.entity.BidBuyRecord;
import com.hb.core.pojo.project.order.dto.UserOrderDTO;
import com.hb.core.pojo.project.order.dto.UserOrderGoodsDTO;
import com.hb.core.pojo.project.order.entity.UserOrderGoods;
import com.hb.core.pojo.project.order.vo.UserOrderExportListVO;
import com.hb.core.pojo.project.order.vo.UserOrderListVO;
import com.hb.core.pojo.project.proj.dto.ProjectBaseInfo;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.api.routine.config.pay.OrderDataConfig;
import com.hb.trade.api.routine.config.pay.WeixinPayBody;
import com.hb.trade.api.routine.sms.annotation.SmsSend;
import com.hb.trade.api.routine.util.SendNewsUtil;
import com.hb.trade.client.center.NewsCenterClient;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.center.UserClient;
import com.hb.trade.client.project.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.AuditStatusEnum;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.model.user.login.LoginUser;
import org.fzzn.core.third.sms.service.SmsService;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hb.core.pojo.enums.PayTypeEnum.ALI_PAY;
import static com.hb.core.pojo.enums.PayTypeEnum.WE_CHAT_PAY;

/**
 * 订单信息 前端控制器
 *
 * @author zhaojk
 * @since 2021-08-30
 */
@Slf4j
@Version(1.0)
@Api(tags = {"订单信息和下载文件"})
@RestController
@RequestMapping("/{version}")
public class ApiUserOrderController {

    protected final UserClient userClient;
    protected final OrgClient orgClient;
    protected final SectionClient sectionClient;
    protected final BidFileInfoClient bidFileInfoClient;
    protected final UserOrderClient userOrderClient;
    protected final ProjectClient projectClient;
    protected final NewsCenterClient newsCenterClient;
    protected final BidWinResultClient bidWinResultClient;
    protected final SmsService smsService;

    public ApiUserOrderController(UserClient userClient,
                                  OrgClient orgClient,
                                  SectionClient sectionClient,
                                  BidFileInfoClient bidFileInfoClient,
                                  UserOrderClient userOrderClient,
                                  ProjectClient projectClient,
                                  NewsCenterClient newsCenterClient,
                                  BidWinResultClient bidWinResultClient, SmsService smsService) {
        this.userClient = userClient;
        this.orgClient = orgClient;
        this.sectionClient = sectionClient;
        this.bidFileInfoClient = bidFileInfoClient;
        this.userOrderClient = userOrderClient;
        this.projectClient = projectClient;
        this.newsCenterClient = newsCenterClient;
        this.bidWinResultClient = bidWinResultClient;
        this.smsService = smsService;
    }

    /**
     * 投标人购买文件
     *
     * @param userOrderDTO
     * @return
     */
    @ApiOperation(value = "投标人购买文件", notes = "投标人购买文件", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "collectionId", value = "收款方ID(该字段为空时调用平台对接的第三方支付)", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "payType", value = "支付方式", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "attachment", value = "付款凭证", dataTypeClass = List.class)
    })
    @PostMapping("/userOrder/purchaseFile")
    public ResponseDto purchaseFile(@RequestBody UserOrderDTO userOrderDTO) {
        Integer payType = userOrderDTO.getPayType();
        PayTypeEnum payTypeEnum = Enums.of(PayTypeEnum.class, payType);
        if (payTypeEnum == null) {
            return ResultUtil.fail("支付方式不能为空");
        }

        OrgDTO extInfo = ShiroUtil.getExtInfo(OrgDTO.class);
        userOrderDTO.setOrgName(extInfo.getOrgName());

        Long userId = ShiroUtil.getUserId();
        userOrderDTO.setUserId(userId);

        Message<User> userMessage = userClient.getByUserId(userId);
        userOrderDTO.setContactName(ShiroUtil.getUserEntity().getFullName());
        userOrderDTO.setMobile(userMessage.getData().getPhone());

        for (UserOrderGoods good : userOrderDTO.getGoods()) {
            good.setOrgId(ShiroUtil.getOrgId());
            good.setFeeType(userOrderDTO.getFeeType());
            good.setSectionId(userOrderDTO.getSectionId());
        }

        if (userOrderDTO.getCollectionId() == null && Enums.equalsAny(payTypeEnum, ALI_PAY, WE_CHAT_PAY)) {
            // 跳转第三方支付, 创建订单
            String orderHeader = OrderDataConfig.orderHeader;
            if (StrUtil.length(orderHeader) > 8) {
                return ResultUtil.fail("请联系管理员订单头,不能超过八位");
            }
            if (!ReUtil.isMatch(OrderDataConfig.orderHeaderReg, orderHeader)) {
                return ResultUtil.fail("订单头只能包含 字母,数字或下划线");
            }
            userOrderDTO.setOutOrderNo(WeixinPayBody.newTradeNo(OrderDataConfig.orderHeader));
            Message<UserOrderDTO> userOrderMsg = userOrderClient.createOrder(userOrderDTO);
            return ResultUtil.msg(userOrderMsg);
        }

        String paramsValid = userOrderDTO.paramsValid();
        if (StrUtil.isNotEmpty(paramsValid)) {
            return ResultUtil.fail(paramsValid);
        }

        userOrderDTO.setOutOrderNo(WeixinPayBody.newTradeNo(OrderDataConfig.orderHeader));

        // 添加订单信息
        Message<?> userOrderMsg = userOrderClient.purchaseFile(userOrderDTO);

        //给招标人发送审核消息
        Long sectionId = userOrderDTO.getSectionId();
        Integer feeType = userOrderDTO.getFeeType();
        SendNewsUtil.sendTendereeNews(
                sectionId,
                NewsTypeEnum.EXPENSE_AUDIT,
                NewsTypeTwoEnum.TENDEREE_EXPENSE_AUDIT,
                Enums.nameOf(FeeTypeEnum.class, feeType),
                SendNewsUtil.getTendereeRoute(feeType),
                SendNewsUtil.getTendereeQuery(feeType)
        );
        return ResultUtil.msg(userOrderMsg);
    }

    @ApiOperation(value = "分页查询购买文件列表", notes = "分页查询购买文件列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sectionName", value = "标段名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sectionCode", value = "标段编号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "participateFlag", value = "是否仅显示可参与的", dataTypeClass = Integer.class),
    })
    @PostMapping("/userOrder/pagePurchaseFile")
    public ResponseDto pagePurchaseFile(@RequestBody UserOrderDTO userOrder) {
        Message<PageResult<UserOrderDTO>> userOrderList = userOrderClient.pagePurchaseFile(userOrder);
        return ResultUtil.msg(userOrderList);
    }


    @ApiOperation(value = "费用管理统计列表", notes = "费用管理列表(该接口的统计结果全部是已支付的订单, 包括第三方和收款码)", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/fee/statistics")
    public ResponseDto feeStatistics(@RequestBody BidSectionDTO sectionDTO) {
        if (sectionDTO.getId() == null && sectionDTO.getProjectId() == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        Integer feeType = sectionDTO.getFeeType();
        if (feeType == null) {
            return ResultUtil.fail("费用类型不能为空");
        }
        Message<?> msg = userOrderClient.feeStatistics(sectionDTO);
        return ResultUtil.msg(msg);
    }

    @ApiOperation(value = "费用管理列表", notes = "费用管理列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/feeManagers")
    public ResponseDto feeManagers(@RequestBody BidSectionDTO sectionDTO) {
        Long sectionId = sectionDTO.getId();
        if (sectionId == null) {
            return ResultUtil.fail("标段ID不能为空");
        }
        Integer feeType = sectionDTO.getFeeType();
        if (feeType == null) {
            return ResultUtil.fail("费用类型不能为空");
        }
        Message<?> userOrderList = userOrderClient.feeManagers(sectionDTO);
        return ResultUtil.msg(userOrderList);
    }

    @ApiOperation(value = "付款凭证 - 审核通过", notes = "付款凭证 - 审核通过", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", dataTypeClass = Long.class)
    })
    @PostMapping("/userOrder/feeManagers/pass")
    public ResponseDto feePass(@RequestBody UserOrderDTO orderDTO) {
        UserOrderDTO userOrderDTO = new UserOrderDTO();
        userOrderDTO.setId(orderDTO.getId());
        userOrderDTO.setAuditStatus(AuditStatusEnum.PASSED.getCode());

        Message<UserOrderDTO> msg = userOrderClient.updateOrderStatus(userOrderDTO);
        UserOrderDTO data = msg.getData();


        // 增加一条购标记录
        BidBuyRecord buyRecord = new BidBuyRecord();
        buyRecord.setBuyUserId(data.getUserId());

        List<UserOrderGoods> goods = data.getGoods();
        buyRecord.setOrgId(goods.get(0).getOrgId());
        buyRecord.setSectionId(goods.get(0).getSectionId());
        buyRecord.setDownTime(LocalDateTime.now());
        buyRecord.setOrderNo(data.getOutOrderNo());
        buyRecord.buildBaseData(1);
        bidFileInfoClient.saveRecord(buyRecord);

        //投标人发消息
        SendNewsUtil.sendBiddereeNews(
                goods.get(0).getSectionId(),
                NewsTypeEnum.EXPENSE_AUDIT,
                NewsTypeTwoEnum.BIDDER_EXPENSE_AUDIT,
                Enums.nameOf(FeeTypeEnum.class, goods.get(0).getFeeType()),
                "已通过",
                SendNewsUtil.getRoute(goods.get(0).getFeeType()),
                CollUtil.list(false, data.getUserId())
        );

        return ResultUtil.msg(msg.isSuccess());
    }

    @ApiOperation(value = "付款凭证 - 审核驳回", notes = "付款凭证 - 审核驳回", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", dataTypeClass = Long.class)
    })
    @PostMapping("/userOrder/feeManagers/reject")
    public ResponseDto feeReject(@RequestBody UserOrderDTO orderDTO) {
        String reason = orderDTO.getReason();
        if (StrUtil.isEmpty(reason)) {
            return ResultUtil.fail("必须填写驳回理由");
        }

        UserOrderDTO userOrderDTO = new UserOrderDTO();
        userOrderDTO.setId(orderDTO.getId());
        userOrderDTO.setAuditStatus(AuditStatusEnum.REJECTED.getCode());
        userOrderDTO.setReason(reason);

        Message<UserOrderDTO> msg = userOrderClient.updateOrderStatus(userOrderDTO);
        UserOrderDTO data = msg.getData();

        List<UserOrderGoods> goods = data.getGoods();


        //投标人发消息
        SendNewsUtil.sendBiddereeNews(
                goods.get(0).getSectionId(),
                NewsTypeEnum.EXPENSE_AUDIT,
                NewsTypeTwoEnum.BIDDER_EXPENSE_AUDIT,
                Enums.nameOf(FeeTypeEnum.class, goods.get(0).getFeeType()),
                "被驳回",
                SendNewsUtil.getRoute(goods.get(0).getFeeType()),
                CollUtil.list(false, data.getUserId())
        );
        return ResultUtil.msg(msg.isSuccess());
    }

    @ApiOperation(value = "支付退款", notes = "支付退款", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", dataTypeClass = Long.class)
    })
    @PostMapping("/userOrder/feeManagers/refund")
    public ResponseDto feeRefund(@RequestBody UserOrderDTO orderDTO) {
        List<Long> ids = orderDTO.getIds();
        if (ArrayUtil.isEmpty(ids)) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }

        UserOrderDTO userOrderDTO = new UserOrderDTO();
        userOrderDTO.setOrderStatus(PayStatusEnum.REFUND.getCode());
        for (Long id : ids) {
            userOrderDTO.setId(id);
            userOrderDTO.setOrderStatus(PayStatusEnum.REFUND.getCode());
            userOrderDTO.setRefundDate(LocalDateTime.now());
            userOrderClient.updateOrderStatus(userOrderDTO);
        }

        return ResultUtil.msg(true);
    }

    @ApiOperation(value = "获取指定费用类型的收费情况", notes = "获取指定费用类型的收费情况", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型", dataTypeClass = String.class)
    })
    @PostMapping("/userOrder/feeInfo")
    public ResponseDto feeInfo(@RequestBody UserOrderGoodsDTO goodsDTO) {
        Integer feeType = goodsDTO.getFeeType();
        FeeTypeEnum feeTypeEnum = Enums.of(FeeTypeEnum.class, feeType);
        if (feeTypeEnum == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        if (goodsDTO.getSectionId() == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        ShiroUtil.fillOwner(goodsDTO);
        Message<JSONObject> feeInfoMsg = userOrderClient.feeInfo(goodsDTO);

        // 查询collectionId详情

        // 商品费用信息和用户的订单信息
        Object data = feeInfoMsg.getData();
        // 转换成JSONObject进行操作
        JSONObject obj = JSONUtil.parseObj(data);

        JSONArray collectionIds = obj.getByPath("orderInfo.collectionIds", JSONArray.class);

        OrgCollectionDTO orgCollectionDTO = new OrgCollectionDTO();
        if (CollUtil.isNotEmpty(collectionIds)) {
            List<OrgCollection> collections = CollUtil.list(false);

            JSONObject orderInfo = obj.getJSONObject("orderInfo");
            for (Object collectionIdObj : collectionIds) {
                long collectionId = NumberUtil.parseLong(collectionIdObj.toString());
                orgCollectionDTO.setId(collectionId);
                Message<OrgCollection> message = orgClient.getOrgCollectionId(orgCollectionDTO);

                if (message.isSuccess()) {
                    OrgCollection orgCollection = message.getData();
                    OrgCollection showOrgCollection = new OrgCollection();

                    showOrgCollection.setCollectionOss(orgCollection.getCollectionOss());
                    showOrgCollection.setId(orgCollection.getId());
                    showOrgCollection.setPayType(orgCollection.getPayType());

                    showOrgCollection.setBankAccount(orgCollection.getBankAccount());
                    showOrgCollection.setBankAccountName(orgCollection.getBankAccountName());
                    showOrgCollection.setBankDeposit(orgCollection.getBankDeposit());
                    collections.add(showOrgCollection);
                }
            }
            orderInfo.putOpt("collections", collections);
            obj.putOpt("orderInfo", orderInfo);
        }

        feeInfoMsg.setData(obj);
        return ResultUtil.msg(feeInfoMsg);
    }


    @ApiOperation(value = "查看订单详情", notes = "查看订单详情", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单ID", dataTypeClass = Long.class)
    })
    @PostMapping("/userOrder/showDetail")
    public ResponseDto orderDetail(@RequestBody UserOrderDTO orderDTO) {
        Long id = orderDTO.getId();
        if (id == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }

        Message<UserOrderDTO> msg = userOrderClient.orderDetail(orderDTO);
        UserOrderDTO data = msg.getData();
        JSONObject dataJSON = JSONUtil.parseObj(data);

        Long sectionId = data.getGoods().get(0).getSectionId();
        if (sectionId != null) {
            Message<ProjectBaseInfo> baseInfoMsg = sectionClient.projectBaseInfo(sectionId);
            dataJSON.putOpt("projectBaseInfo", baseInfoMsg.getData());
        }

        return ResultUtil.msg(true, dataJSON);
    }


    @ApiOperation(value = "递交投标保证金和履约保证金", notes = "递交投标保证金和履约保证金", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段ID", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(name = "payType", value = "支付类型(支付类型 - 1: 支付宝, 2: 微信, 3: 银联)", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "contactName", value = "联系人", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "mobile", value = "移动电话", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "email", value = "邮箱", dataTypeClass = String.class),
            @ApiImplicitParam(name = "tel", value = "固话", dataTypeClass = String.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型(1: 投标保证金, 2: 履约保证金)", dataTypeClass = Integer.class),

            @ApiImplicitParam(name = "refundInfo.bankAccount", value = "退款账号", dataTypeClass = String.class),
            @ApiImplicitParam(name = "refundInfo.bankUsername", value = "退款户名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "refundInfo.bankName", value = "开户行", dataTypeClass = String.class),
            @ApiImplicitParam(name = "refundInfo.contactName", value = "联系人名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "refundInfo.mobile", value = "联系方式", dataTypeClass = String.class),
            @ApiImplicitParam(name = "refundInfo.paymentWay", value = "保证金缴纳方式", dataTypeClass = Integer.class)
    })
    @SmsSend(type = 7)
    @PostMapping("/userOrder/submitBond")
    public ResponseDto submitBond(@RequestBody UserOrderDTO order) {
        String paramsValid = order.paramsValid();
        if (StrUtil.isNotEmpty(paramsValid)) {
            return ResultUtil.fail(paramsValid);
        }

        // 如果是履约保证金 则需要判断是否是中标人
        if (null != order.getFeeType() && order.getFeeType() == 2) {
            BidWinResultDTO bidWinResultDTO = new BidWinResultDTO();
            bidWinResultDTO.setSectionId(order.getSectionId());
            bidWinResultDTO.setOrgId(ShiroUtil.getOrgId());
            Message<Boolean> type = bidWinResultClient.findWinResultSupplyBySectionIdAndOrgId(bidWinResultDTO);
            if (type.isSuccess() && !type.getData()) {
                return ResultUtil.fail("当前企业未中标，不需要提交履约保证金");
            }
        }

        List<UserOrderGoods> goods = order.getGoods();

        for (UserOrderGoods good : goods) {
            good.setSectionId(order.getSectionId());
            good.setOrgId(ShiroUtil.getOrgId());
            good.setFeeType(order.getFeeType());
        }

        order.setUserId(ShiroUtil.getUserId());
        order.setOrgName(ShiroUtil.getExtInfo(OrgDTO.class).getOrgName());
        order.setOutOrderNo(WeixinPayBody.newTradeNo());
        order.setOrderStatus(PayStatusEnum.PAY_SUCCESS.getCode());
        order.setAuditStatus(AuditStatusEnum.WAITING.getCode());

        Message<?> msg = userOrderClient.saveBondInfo(order);
        //给招标人发送审核消息
        Long sectionId = order.getSectionId();
        Integer feeType = order.getFeeType();

        SendNewsUtil.sendTendereeNews(
                sectionId,
                NewsTypeEnum.EXPENSE_AUDIT,
                NewsTypeTwoEnum.TENDEREE_EXPENSE_AUDIT,
                Enums.nameOf(FeeTypeEnum.class, feeType),
                SendNewsUtil.getTendereeRoute(feeType),
                SendNewsUtil.getTendereeQuery(feeType)
        );
        return ResultUtil.msg(msg);
    }

    @ApiOperation(value = "履约保证金管理列表", notes = "履约保证金管理列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型(1: 投标保证金, 2: 履约保证金)", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/getPrice")
    public ResponseDto getPrice(@RequestBody UserOrderGoodsDTO goodsDTO) {
        Message<BigDecimal> msg = userOrderClient.getPrice(goodsDTO.getSectionId(), goodsDTO.getFeeType());
        return ResultUtil.msg(msg);
    }

    /**
     * 保证金管理列表
     * 包含投标保证金和履约保证金
     *
     * @param goodsDTO
     * @return
     */
    @ApiOperation(value = "保证金管理列表", notes = "保证金管理列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "sectionId", value = "标段ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orgName", value = "企业名称(查询条件)", dataTypeClass = String.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型(1: 投标保证金, 2: 履约保证金)", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/bondManager")
    public ResponseDto bondManager(@RequestBody UserOrderGoodsDTO goodsDTO) {
        if (goodsDTO.getSectionId() == null && goodsDTO.getProjectId() == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        JSONObject extInfo = ShiroUtil.getExtInfo();
        Integer roleSign = extInfo.getInt("roleSign");
        RoleSignEnum roleSignEnum = Enums.of(RoleSignEnum.class, roleSign);

        Message<?> msg;

        switch (roleSignEnum) {
            case BIDDER: // 投标人
                goodsDTO.setOrgId(ShiroUtil.getOrgId());
                msg = userOrderClient.pageRecord(goodsDTO);
                return ResultUtil.msg(msg);
            case TENDEREE: // 招标人
            case AGENCY: // 代理机构
                msg = userOrderClient.pageRecord(goodsDTO);
                return ResultUtil.msg(msg);
        }
        return ResultUtil.FAIL;
    }


    /**
     * Description: 判断当前企业是否已购买标书
     *
     * @param bidBuyRecord
     * @author: fengbin
     * @date: 2022/2/28 15:27
     * @return: org.fzzn.core.model.msg.Message<java.lang.Boolean>
     */
    @PostMapping("/bidBuyRecord/judgeCompanyRecord")
    public ResponseDto judgeCompanyRecord(@RequestBody BidBuyRecord bidBuyRecord) {
        LoginUser<Long> loginUser = ShiroUtil.getUser();
        if (null == loginUser) {
            return ResultUtil.fail(MsgEnum.LOGIN_EXPIRE);
        }
        bidBuyRecord.setBuyUserId(ShiroUtil.getUserId());
        bidBuyRecord.setOrgId(ShiroUtil.getOrgId());
        Message<Boolean> booleanMessage = bidFileInfoClient.judgeCompanyRecord(bidBuyRecord);
        return ResultUtil.msg(booleanMessage);
    }


    @ApiOperation(value = "费用订单列表", notes = "费用订单列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段ID", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "feeType", value = "费用类型(1: 投标保证金  2: 履约保证金  3: 报名费  4: 标书管理费 5 系统使用费 6 其他商品)", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/listUserOrderByFreeType")
    public ResponseDto listUserOrderByFreeType(@RequestBody UserOrderGoodsDTO goodsDTO) {
        ShiroUtil.fillOwner(goodsDTO);
        Message<List<UserOrderDTO>> userOrderMsg = userOrderClient.listUserOrderByFreeType(goodsDTO);
        return ResultUtil.msg(userOrderMsg);
    }

    /**
     * 已支付订单分页列表
     *
     * @param userOrderDTO
     * @return Message<PageResult < UserOrderListVO>>
     * @author yuezheng
     * @date 2022/4/13 11:37 PM
     */
    @ApiOperation(value = "订单已支付列表", notes = "订单已支付列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orgName", value = "代理公司", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "outOrderNo", value = "流水号", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "source", value = "渠道来源", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "payTimeStart", value = "交易开始时间", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "payTimeEnd", value = "交易结束时间", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/listSuccess")
    public ResponseDto listSuccessUserOrder(@RequestBody UserOrderDTO userOrderDTO) {
        Message<PageResult<UserOrderListVO>> resultMessage = userOrderClient.listSuccessUserOrder(userOrderDTO);
        return ResultUtil.msg(resultMessage);
    }

    @ApiOperation(value = "本月金额统计", notes = "本月金额统计", httpMethod = "POST")
    @PostMapping("/userOrder/count/amount")
    ResponseDto<BigDecimal> selectOrderAmountCountToMonth(@RequestBody UserOrderDTO userOrderDTO) {
        Message<BigDecimal> message = userOrderClient.selectOrderAmountCountToMonth(userOrderDTO);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "本月订单统计", notes = "本月订单统计", httpMethod = "POST")
    @PostMapping("/userOrder/count")
    ResponseDto<Integer> selectOrderCountToMonth(@RequestBody UserOrderDTO userOrderDTO) {
        Message<Integer> message = userOrderClient.selectOrderCountToMonth(userOrderDTO);
        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "订单导出", notes = "订单导出", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orgName", value = "代理公司", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "outOrderNo", value = "流水号", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "source", value = "渠道来源", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "payTimeStart", value = "交易开始时间", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "payTimeEnd", value = "交易结束时间", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/export/check")
    ResponseDto exportExcelCheck(@RequestBody UserOrderDTO userOrderDTO) {
        Message<List<UserOrderExportListVO>> message = userOrderClient.selectExportExcelList(userOrderDTO);
        if (message.getCode() == 0) {
            if (message.getMessage().equals("org")) {
                return ResultUtil.fail("最多导出一家代理公司");
            }
            if (message.getMessage().equals("pro")) {
                return ResultUtil.fail("导出项目最多不超过13个");
            }
        }
        if (message.getData().size() < 1) {
            return ResultUtil.fail("至少有一条数据");
        }
        return ResultUtil.msg(true);
    }


    @ApiOperation(value = "订单已支付列表", notes = "订单已支付列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectName", value = "项目名称", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "orgName", value = "代理公司", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "outOrderNo", value = "流水号", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "source", value = "渠道来源", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "payTimeStart", value = "交易开始时间", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "payTimeEnd", value = "交易结束时间", dataTypeClass = Integer.class)
    })
    @PostMapping("/userOrder/export")
    ResponseDto exportExcel(@RequestBody UserOrderDTO userOrderDTO, HttpServletResponse response) {

        Message<List<UserOrderExportListVO>> message = userOrderClient.selectExportExcelList(userOrderDTO);

        List<UserOrderExportListVO> data = message.getData();
        final String projectOrgName = data.get(0).getProjectOrgName();
        final String settlementTime = DateUtil.format(LocalDateTime.now(), "yyyy年MM月dd日");

        Set<Long> projectSet = data.stream().map(UserOrderExportListVO::getProjectId).collect(Collectors.toSet());

        HashMap<Long, List<UserOrderExportListVO>> orderMap = new HashMap<>();

        // 根据项目分组
        for (UserOrderExportListVO item : data) {
            if (orderMap.containsKey(item.getProjectId())) {
                List<UserOrderExportListVO> list = orderMap.get(item.getProjectId());
                list.add(item);
                orderMap.put(item.getProjectId(), list);
            } else {
                ArrayList<UserOrderExportListVO> newList = new ArrayList<>();
                newList.add(item);
                orderMap.put(item.getProjectId(), newList);
            }

        }


        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            /* ====================================== 结算总清单 ====================================== */
            HSSFSheet sheet = wb.createSheet("结算总清单");
            sheet.setColumnWidth(0, 3500);
            sheet.setColumnWidth(1, 8200);
            sheet.setColumnWidth(2, 6200);
            sheet.setColumnWidth(3, 6200);
            sheet.setColumnWidth(4, 6200);
            // 标题
            HSSFRow row00 = sheet.createRow(0);
            row00.setHeight((short) (40 * 20));
            HSSFCell cell00 = row00.createCell(0);
            cell00.setCellValue("赢标结算单");
            HSSFCellStyle cellStyle00 = cell00.getCellStyle();

            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
            //设置水平对齐的样式为居中对齐;
            cellStyle00.setAlignment(HorizontalAlignment.CENTER);
            //设置垂直对齐的样式为居中对齐;
            cellStyle00.setVerticalAlignment(VerticalAlignment.CENTER);
            cell00.setCellStyle(cellStyle00);

            // 第二行
            HSSFRow row10 = sheet.createRow(1);
            row10.setHeight((short) (40 * 20));
            HSSFCell cell10 = row10.createCell(0);
            cell10.setCellValue("代理公司:" + projectOrgName);
            HSSFCellStyle cellStyle10 = cell10.getCellStyle();
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
            cellStyle10.setAlignment(HorizontalAlignment.CENTER);
            cellStyle10.setVerticalAlignment(VerticalAlignment.CENTER);
            cell10.setCellStyle(cellStyle10);

            HSSFCell cell12 = row10.createCell(2);
            cell12.setCellValue("结算时间：" + settlementTime);
            HSSFCellStyle cellStyle12 = cell12.getCellStyle();
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 4));
            cellStyle12.setAlignment(HorizontalAlignment.CENTER);
            cellStyle12.setVerticalAlignment(VerticalAlignment.CENTER);
            cell12.setCellStyle(cellStyle12);

            // 第三行
            HSSFRow row2 = sheet.createRow(2);
            row2.setHeight((short) (40 * 20));

            HSSFCell cell20 = row2.createCell(0);
            cell20.setCellValue("序号");
            HSSFCellStyle cellStyle20 = cell20.getCellStyle();
            cellStyle20.setAlignment(HorizontalAlignment.CENTER);
            cellStyle20.setVerticalAlignment(VerticalAlignment.CENTER);
            cell20.setCellStyle(cellStyle20);

            HSSFCell cell21 = row2.createCell(1);
            cell21.setCellValue("项目名称");
            HSSFCellStyle cellStyle21 = cell21.getCellStyle();
            cellStyle21.setAlignment(HorizontalAlignment.CENTER);
            cellStyle21.setVerticalAlignment(VerticalAlignment.CENTER);
            cell21.setCellStyle(cellStyle21);

            HSSFCell cell22 = row2.createCell(2);
            cell22.setCellValue("文件费");
            HSSFCellStyle cellStyle22 = cell22.getCellStyle();
            cellStyle22.setAlignment(HorizontalAlignment.CENTER);
            cellStyle22.setVerticalAlignment(VerticalAlignment.CENTER);
            cell22.setCellStyle(cellStyle22);

            HSSFCell cell23 = row2.createCell(3);
            cell23.setCellValue("平台使用费");
            HSSFCellStyle cellStyle23 = cell23.getCellStyle();
            cellStyle23.setAlignment(HorizontalAlignment.CENTER);
            cellStyle23.setVerticalAlignment(VerticalAlignment.CENTER);
            cell23.setCellStyle(cellStyle23);

            HSSFCell cell24 = row2.createCell(4);
            cell24.setCellValue("需要提供发票金额");
            HSSFCellStyle cellStyle24 = cell24.getCellStyle();
            cellStyle24.setAlignment(HorizontalAlignment.CENTER);
            cellStyle24.setVerticalAlignment(VerticalAlignment.CENTER);
            cell24.setCellStyle(cellStyle24);

            // 数据行
            int curr = 0;
            Double sumFilePrice = 0.0;
            Double sumSystemPrice = 0.0;
            for (Long key : projectSet) {
                curr++;

                Double filePrice = 0.0;
                Double systemPrice = 0.0;

                List<UserOrderExportListVO> userOrderProjectItemList = orderMap.get(key);
                for (UserOrderExportListVO item : userOrderProjectItemList) {
                    switch (item.getFeeType()) {
                        case 4:  // 标书费
                            filePrice += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                            sumFilePrice += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                            break;
                        case 5:  // 项目管理费
                            systemPrice += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                            sumSystemPrice += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                            break;
                    }
                }
                HSSFRow row = sheet.createRow(curr + 2);
                row.setHeight((short) (40 * 20));

                HSSFCell cell0 = row.createCell(0);
                cell0.setCellValue(curr);
                HSSFCellStyle cellStyle0 = cell0.getCellStyle();
                //设置水平对齐的样式为居中对齐;
                cellStyle0.setAlignment(HorizontalAlignment.CENTER);
                //设置垂直对齐的样式为居中对齐;
                cellStyle0.setVerticalAlignment(VerticalAlignment.CENTER);
                cell0.setCellStyle(cellStyle20);

                HSSFCell cell1 = row.createCell(1);
                cell1.setCellValue(orderMap.get(key).get(0).getProjectName());
                HSSFCellStyle cellStyle1 = cell1.getCellStyle();
                //设置水平对齐的样式为居中对齐;
                cellStyle1.setAlignment(HorizontalAlignment.CENTER);
                //设置垂直对齐的样式为居中对齐;
                cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
                cell1.setCellStyle(cellStyle21);

                HSSFCell cell2 = row.createCell(2);
                cell2.setCellValue(filePrice);
                HSSFCellStyle cellStyle2 = cell2.getCellStyle();
                //设置水平对齐的样式为居中对齐;
                cellStyle2.setAlignment(HorizontalAlignment.CENTER);
                //设置垂直对齐的样式为居中对齐;
                cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
                cell2.setCellStyle(cellStyle22);

                HSSFCell cell3 = row.createCell(3);
                cell3.setCellValue(systemPrice);
                HSSFCellStyle cellStyle3 = cell3.getCellStyle();
                //设置水平对齐的样式为居中对齐;
                cellStyle3.setAlignment(HorizontalAlignment.CENTER);
                //设置垂直对齐的样式为居中对齐;
                cellStyle3.setVerticalAlignment(VerticalAlignment.CENTER);
                cell3.setCellStyle(cellStyle3);
            }

            // 合计
            HSSFRow row = sheet.createRow(curr + 3);
            row.setHeight((short) (40 * 20));
            HSSFCell sumCell = row.createCell(1);
            sumCell.setCellValue("合计");
            HSSFCellStyle sumStyle = sumCell.getCellStyle();
            sumStyle.setAlignment(HorizontalAlignment.CENTER);
            sumStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            sumCell.setCellStyle(sumStyle);

            HSSFCell sumFileCell = row.createCell(2);
            sumFileCell.setCellValue(sumFilePrice);
            HSSFCellStyle sumFileStyle = sumCell.getCellStyle();
            sumFileStyle.setAlignment(HorizontalAlignment.CENTER);
            sumFileStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            sumFileCell.setCellStyle(sumFileStyle);

            HSSFCell sumSystemCell = row.createCell(3);
            sumSystemCell.setCellValue(sumSystemPrice);
            HSSFCellStyle sumSystemStyle = sumCell.getCellStyle();
            sumSystemStyle.setAlignment(HorizontalAlignment.CENTER);
            sumSystemStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            sumSystemCell.setCellStyle(sumSystemStyle);

            // 结算
            HSSFRow tailRow = sheet.createRow(curr + 4);
            tailRow.setHeight((short) (40 * 20));
            HSSFCell tailCell = tailRow.createCell(0);
            tailCell.setCellValue("结算账户");
            HSSFCellStyle tailStyle = tailCell.getCellStyle();
            sheet.addMergedRegion(new CellRangeAddress(curr + 4, curr + 4, 0, 1));
            tailStyle.setAlignment(HorizontalAlignment.CENTER);
            tailStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            tailCell.setCellStyle(tailStyle);

            HSSFCell tailCell1 = tailRow.createCell(2);
            tailCell1.setCellValue("结算依据：执行双方签订合作协议");
            HSSFCellStyle tailStyle1 = tailCell1.getCellStyle();
            sheet.addMergedRegion(new CellRangeAddress(curr + 4, curr + 4, 2, 4));
            cellStyle12.setAlignment(HorizontalAlignment.CENTER);
            cellStyle12.setVerticalAlignment(VerticalAlignment.CENTER);
            cell12.setCellStyle(tailStyle1);

            sheet.addMergedRegion(new CellRangeAddress(curr + 5, curr + 6, 0, 1));
            sheet.addMergedRegion(new CellRangeAddress(curr + 5, curr + 5, 2, 4));
            sheet.addMergedRegion(new CellRangeAddress(curr + 6, curr + 6, 2, 4));
            HSSFRow r1 = sheet.createRow(curr + 5);
            r1.setHeight((short) (40 * 20));
            HSSFRow r2 = sheet.createRow(curr + 6);
            r2.setHeight((short) (40 * 20));

            /* ====================================== 项目结算单 ====================================== */

            for (Long key : projectSet) {

                HSSFSheet projectSheet = wb.createSheet(orderMap.get(key).get(0).getProjectName());
                projectSheet.setColumnWidth(0, 3500);
                projectSheet.setColumnWidth(1, 8200);
                projectSheet.setColumnWidth(2, 6200);
                projectSheet.setColumnWidth(3, 6200);
                projectSheet.setColumnWidth(4, 6200);
                // 标题
                HSSFRow rows00 = projectSheet.createRow(0);
                rows00.setHeight((short) (40 * 20));
                HSSFCell cells00 = rows00.createCell(0);
                cells00.setCellValue("赢标" + orderMap.get(key).get(0).getProjectName() + "(" + orderMap.get(key).get(0).getProjectId() + ")" + "结算单");
                HSSFCellStyle cellStyles00 = cell00.getCellStyle();

                projectSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
                //设置水平对齐的样式为居中对齐;
                cellStyles00.setAlignment(HorizontalAlignment.CENTER);
                //设置垂直对齐的样式为居中对齐;
                cellStyles00.setVerticalAlignment(VerticalAlignment.CENTER);
                cells00.setCellStyle(cellStyles00);

                // 第二行
                HSSFRow rows10 = projectSheet.createRow(1);
                rows10.setHeight((short) (40 * 20));
                HSSFCell cells10 = rows10.createCell(0);
                cells10.setCellValue("代理公司:" + projectOrgName);
                HSSFCellStyle cellStyles10 = cells10.getCellStyle();
                projectSheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 1));
                cellStyles10.setAlignment(HorizontalAlignment.CENTER);
                cellStyles10.setVerticalAlignment(VerticalAlignment.CENTER);
                cells10.setCellStyle(cellStyles10);

                HSSFCell cells12 = rows10.createCell(2);
                cells12.setCellValue("结算时间：" + settlementTime);
                HSSFCellStyle cellStyles12 = cells12.getCellStyle();
                projectSheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 4));
                cellStyles12.setAlignment(HorizontalAlignment.CENTER);
                cellStyles12.setVerticalAlignment(VerticalAlignment.CENTER);
                cells12.setCellStyle(cellStyles12);

                // 第三行
                HSSFRow rows2 = projectSheet.createRow(2);
                rows2.setHeight((short) (40 * 20));

                HSSFCell cells20 = rows2.createCell(0);
                cells20.setCellValue("序号");
                HSSFCellStyle cellStyles20 = cells20.getCellStyle();
                cellStyles20.setAlignment(HorizontalAlignment.CENTER);
                cellStyles20.setVerticalAlignment(VerticalAlignment.CENTER);
                cells20.setCellStyle(cellStyles20);

                HSSFCell cells21 = rows2.createCell(1);
                cells21.setCellValue("投标人");
                HSSFCellStyle cellStyles21 = cells21.getCellStyle();
                cellStyles21.setAlignment(HorizontalAlignment.CENTER);
                cellStyles21.setVerticalAlignment(VerticalAlignment.CENTER);
                cells21.setCellStyle(cellStyles21);

                HSSFCell cells22 = rows2.createCell(2);
                cells22.setCellValue("文件费");
                HSSFCellStyle cellStyles22 = cells22.getCellStyle();
                cellStyles22.setAlignment(HorizontalAlignment.CENTER);
                cellStyles22.setVerticalAlignment(VerticalAlignment.CENTER);
                cells22.setCellStyle(cellStyles22);

                HSSFCell cells23 = rows2.createCell(3);
                cells23.setCellValue("平台使用费");
                HSSFCellStyle cellStyles23 = cells23.getCellStyle();
                cellStyles23.setAlignment(HorizontalAlignment.CENTER);
                cellStyles23.setVerticalAlignment(VerticalAlignment.CENTER);
                cells23.setCellStyle(cellStyles23);

                HSSFCell cells24 = rows2.createCell(4);
                cells24.setCellValue("备注");
                HSSFCellStyle cellStyles24 = cells24.getCellStyle();
                cellStyles24.setAlignment(HorizontalAlignment.CENTER);
                cellStyles24.setVerticalAlignment(VerticalAlignment.CENTER);
                cells24.setCellStyle(cellStyles24);

                // 数据行
                int currs = 0;
                Double sumFilePrices = 0.0;
                Double sumSystemPrices = 0.0;
                HashMap<Long, List<UserOrderExportListVO>> orgMap = new HashMap();

                List<UserOrderExportListVO> orderExportListVOS = orderMap.get(key);
                Set<Long> orgSet = orderExportListVOS.stream().map(UserOrderExportListVO::getOrgId).collect(Collectors.toSet());

                for (UserOrderExportListVO item : orderExportListVOS) {
                    if (orgMap.containsKey(item.getOrgId())) {
                        List<UserOrderExportListVO> list = orgMap.get(item.getOrgId());
                        list.add(item);
                        orgMap.put(item.getOrgId(), list);
                    } else {
                        ArrayList<UserOrderExportListVO> newList = new ArrayList<>();
                        newList.add(item);
                        orgMap.put(item.getOrgId(), newList);
                    }
                }

                for (Long orgId : orgSet) {
                    currs++;
                    Double filePrice = 0.0;
                    Double systemPrice = 0.0;

                    for (UserOrderExportListVO item : orgMap.get(orgId)) {
                        if (item.getOrgId().equals(orgId)) {
                            switch (item.getFeeType()) {
                                case 4:  // 标书费
                                    filePrice += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                                    sumFilePrices += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                                    ;
                                    break;
                                case 5:  // 项目管理费
                                    systemPrice += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                                    sumSystemPrices += item.getGoodPrice().doubleValue() * (item.getGoodTotal() == null ? 1 : item.getGoodTotal());
                                    break;
                            }
                        }
                    }

                    HSSFRow rows = projectSheet.createRow(currs + 2);
                    rows.setHeight((short) (40 * 20));

                    HSSFCell cells0 = rows.createCell(0);
                    cells0.setCellValue(currs);
                    HSSFCellStyle cellStyles0 = cells0.getCellStyle();
                    //设置水平对齐的样式为居中对齐;
                    cellStyles0.setAlignment(HorizontalAlignment.CENTER);
                    //设置垂直对齐的样式为居中对齐;
                    cellStyles0.setVerticalAlignment(VerticalAlignment.CENTER);
                    cells0.setCellStyle(cellStyles20);

                    HSSFCell cells1 = rows.createCell(1);
                    cells1.setCellValue(orgMap.get(orgId).get(0).getOrgName());
                    HSSFCellStyle cellStyles1 = cells1.getCellStyle();
                    //设置水平对齐的样式为居中对齐;
                    cellStyles1.setAlignment(HorizontalAlignment.CENTER);
                    //设置垂直对齐的样式为居中对齐;
                    cellStyles1.setVerticalAlignment(VerticalAlignment.CENTER);
                    cells1.setCellStyle(cellStyles21);

                    HSSFCell cells2 = rows.createCell(2);
                    cells2.setCellValue(filePrice);
                    HSSFCellStyle cellStyles2 = cells2.getCellStyle();
                    //设置水平对齐的样式为居中对齐;
                    cellStyles2.setAlignment(HorizontalAlignment.CENTER);
                    //设置垂直对齐的样式为居中对齐;
                    cellStyles2.setVerticalAlignment(VerticalAlignment.CENTER);
                    cells2.setCellStyle(cellStyles22);

                    HSSFCell cells3 = rows.createCell(3);
                    cells3.setCellValue(systemPrice);
                    HSSFCellStyle cellStyles3 = cells3.getCellStyle();
                    //设置水平对齐的样式为居中对齐;
                    cellStyles3.setAlignment(HorizontalAlignment.CENTER);
                    //设置垂直对齐的样式为居中对齐;
                    cellStyles3.setVerticalAlignment(VerticalAlignment.CENTER);
                    cells3.setCellStyle(cellStyles3);
                }
                // 合计
                HSSFRow rows = projectSheet.createRow(currs + 3);
                rows.setHeight((short) (40 * 20));
                HSSFCell sumCells = rows.createCell(1);
                sumCells.setCellValue("合计");
                HSSFCellStyle sumStyles = sumCells.getCellStyle();
                sumStyles.setAlignment(HorizontalAlignment.CENTER);
                sumStyles.setVerticalAlignment(VerticalAlignment.CENTER);
                sumCells.setCellStyle(sumStyles);

                HSSFCell sumFileCells = rows.createCell(2);
                sumFileCells.setCellValue(sumFilePrices);
                HSSFCellStyle sumFileStyles = sumCells.getCellStyle();
                sumFileStyles.setAlignment(HorizontalAlignment.CENTER);
                sumFileStyles.setVerticalAlignment(VerticalAlignment.CENTER);
                sumFileCells.setCellStyle(sumFileStyles);

                HSSFCell sumSystemCells = rows.createCell(3);
                sumSystemCells.setCellValue(sumSystemPrices);
                HSSFCellStyle sumSystemStyles = sumCells.getCellStyle();
                sumSystemStyles.setAlignment(HorizontalAlignment.CENTER);
                sumSystemStyles.setVerticalAlignment(VerticalAlignment.CENTER);
                sumSystemCells.setCellStyle(sumSystemStyles);

                // 结算
                HSSFRow tailRows = projectSheet.createRow(currs + 4);
                tailRows.setHeight((short) (40 * 20));
                HSSFCell tailCells = tailRows.createCell(0);
                tailCells.setCellValue("结算账户");
                HSSFCellStyle tailStyles = tailCells.getCellStyle();
                projectSheet.addMergedRegion(new CellRangeAddress(currs + 4, currs + 4, 0, 1));
                tailStyles.setAlignment(HorizontalAlignment.CENTER);
                tailStyles.setVerticalAlignment(VerticalAlignment.CENTER);
                tailCells.setCellStyle(tailStyles);

                HSSFCell tailCells1 = tailRows.createCell(2);
                tailCells1.setCellValue("结算依据：执行双方签订合作协议");
                HSSFCellStyle tailStyles1 = tailCells1.getCellStyle();
                projectSheet.addMergedRegion(new CellRangeAddress(currs + 4, currs + 4, 2, 4));
                cellStyles12.setAlignment(HorizontalAlignment.CENTER);
                cellStyles12.setVerticalAlignment(VerticalAlignment.CENTER);
                cells12.setCellStyle(tailStyles1);

                projectSheet.addMergedRegion(new CellRangeAddress(currs + 5, currs + 6, 0, 1));
                projectSheet.addMergedRegion(new CellRangeAddress(currs + 5, currs + 5, 2, 4));
                projectSheet.addMergedRegion(new CellRangeAddress(currs + 6, currs + 6, 2, 4));
                HSSFRow rs1 = projectSheet.createRow(currs + 5);
                rs1.setHeight((short) (40 * 20));
                HSSFRow rs2 = projectSheet.createRow(currs + 6);
                rs2.setHeight((short) (40 * 20));


            }

            String title = "赢标结算单";
            response.setHeader("Content-disposition", "attachment;fileName=" + title + ".xls");
            response.setContentType("application/octet-stream;charset=utf-8");
            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();

            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return ResultUtil.fail("导出失败");
        }
    }

    /**
     * 判断用户订单是否支付
     *
     * @param userOrderDTO
     * @return
     */
    @PostMapping("/userOrder/OrderDetail")
    public ResponseDto<?> OrderDetail(@RequestBody UserOrderDTO userOrderDTO) {
        if (userOrderDTO == null || userOrderDTO.getSectionId() == null) {
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }
        if (userOrderDTO.getFeeType() == null) {
            return ResultUtil.fail(MsgEnum.INCORRECT_PARAM);
        }
        Long useId = ShiroUtil.getUserId();
        userOrderDTO.setUserId(useId);
        userOrderDTO.setOrgId(ShiroUtil.getOrgId());
        Message<Boolean> result = userOrderClient.OrderPayStatus(userOrderDTO);
        JSONObject jsonObject = new JSONObject();
        if (result.isSuccess()) {
            jsonObject.put("payFlag", 1);
        } else {
            jsonObject.put("payFlag", 2);
        }
        return ResultUtil.msg(true, jsonObject);

    }
}
